home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / timidsrc.zip / instrum.c < prev    next >
C/C++ Source or Header  |  1996-05-20  |  17KB  |  668 lines

  1. /*
  2.  
  3.     TiMidity -- Experimental MIDI to WAVE converter
  4.     Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.    instrum.c 
  21.    
  22.    Code to load and unload GUS-compatible instrument patches.
  23.  
  24. */
  25.  
  26. #include <stdio.h>
  27.  
  28. #if defined(SOLARIS) || defined(__WIN32__)
  29. #include <string.h>
  30. #else
  31. #include <strings.h>
  32. #endif
  33.  
  34. #if defined(__FreeBSD__) || defined (__WIN32__)
  35. #include <stdlib.h>
  36. #else
  37. #include <malloc.h>
  38. #endif
  39.  
  40. #include "config.h"
  41. #include "common.h"
  42. #include "instrum.h"
  43. #include "playmidi.h"
  44. #include "output.h"
  45. #include "controls.h"
  46. #include "resample.h"
  47. #include "tables.h"
  48. #include "filter.h"
  49.  
  50. /* Some functions get aggravated if not even the standard banks are 
  51.    available. */
  52. static ToneBank standard_tonebank, standard_drumset;
  53. ToneBank 
  54.   *tonebank[128]={&standard_tonebank},
  55.   *drumset[128]={&standard_drumset};
  56.  
  57. /* This is a special instrument, used for all melodic programs */
  58. Instrument *default_instrument=0;
  59.  
  60. /* This is only used for tracks that don't specify a program */
  61. int default_program=DEFAULT_PROGRAM;
  62.  
  63. int antialiasing_allowed=0;
  64. #ifdef FAST_DECAY
  65. int fast_decay=1;
  66. #else
  67. int fast_decay=0;
  68. #endif
  69.  
  70. static void free_instrument(Instrument *ip)
  71. {
  72.   Sample *sp;
  73.   int i;
  74.   if (!ip) return;
  75.   for (i=0; i<ip->samples; i++)
  76.     {
  77.       sp=&(ip->sample[i]);
  78.       free(sp->data);
  79.     }
  80.   free(ip->sample);
  81.   free(ip);
  82. }
  83.  
  84. static void free_bank(int dr, int b)
  85. {
  86.   int i;
  87.   ToneBank *bank=((dr) ? drumset[b] : tonebank[b]);
  88.   for (i=0; i<128; i++)
  89.     if (bank->tone[i].instrument)
  90.       {
  91.     /* Not that this could ever happen, of course */
  92.     if (bank->tone[i].instrument != MAGIC_LOAD_INSTRUMENT)
  93.       free_instrument(bank->tone[i].instrument);
  94.     bank->tone[i].instrument=0;
  95.       }
  96. }
  97.  
  98. static int32 convert_envelope_rate(uint8 rate)
  99. {
  100.   int32 r;
  101.  
  102.   r=3-((rate>>6) & 0x3);
  103.   r*=3;
  104.   r = (int32)(rate & 0x3f) << r; /* 6.9 fixed point */
  105.  
  106.   /* 15.15 fixed point. */
  107.   return (((r * 44100) / play_mode->rate) * control_ratio) 
  108.     << ((fast_decay) ? 10 : 9);
  109. }
  110.  
  111. static int32 convert_envelope_offset(uint8 offset)
  112. {
  113.   /* This is not too good... Can anyone tell me what these values mean?
  114.      Are they GUS-style "exponential" volumes? And what does that mean? */
  115.  
  116.   /* 15.15 fixed point */
  117.   return offset << (7+15);
  118. }
  119.  
  120. static int32 convert_tremolo_sweep(uint8 sweep)
  121. {
  122.   if (!sweep)
  123.     return 0;
  124.  
  125.   return
  126.     ((control_ratio * SWEEP_TUNING) << SWEEP_SHIFT) /
  127.       (play_mode->rate * sweep);
  128. }
  129.  
  130. static int32 convert_vibrato_sweep(uint8 sweep, int32 vib_control_ratio)
  131. {
  132.   if (!sweep)
  133.     return 0;
  134.  
  135.   return
  136.     (int32) (FSCALE((double) (vib_control_ratio) * SWEEP_TUNING, SWEEP_SHIFT)
  137.          / (double)(play_mode->rate * sweep));
  138.  
  139.   /* this was overflowing with seashore.pat
  140.  
  141.       ((vib_control_ratio * SWEEP_TUNING) << SWEEP_SHIFT) /
  142.       (play_mode->rate * sweep); */
  143. }
  144.  
  145. static int32 convert_tremolo_rate(uint8 rate)
  146. {
  147.   return
  148.     ((SINE_CYCLE_LENGTH * control_ratio * rate) << RATE_SHIFT) /
  149.       (TREMOLO_RATE_TUNING * play_mode->rate);
  150. }
  151.  
  152. static int32 convert_vibrato_rate(uint8 rate)
  153. {
  154.   /* Return a suitable vibrato_control_ratio value */
  155.   return
  156.     (VIBRATO_RATE_TUNING * play_mode->rate) / 
  157.       (rate * 2 * VIBRATO_SAMPLE_INCREMENTS);
  158. }
  159.  
  160. static void reverse_data(int16 *sp, int32 ls, int32 le)
  161. {
  162.   int16 s, *ep=sp+le;
  163.   sp+=ls;
  164.   le-=ls;
  165.   le/=2;
  166.   while (le--)
  167.     {
  168.       s=*sp;
  169.       *sp++=*ep;
  170.       *ep--=s;
  171.     }
  172. }
  173.  
  174. /* 
  175.    If panning or note_to_use != -1, it will be used for all samples,
  176.    instead of the sample-specific values in the instrument file. 
  177.  
  178.    For note_to_use, any value <0 or >127 will be forced to 0.
  179.  
  180.    For other parameters, 1 means yes, 0 means no, other values are
  181.    undefined.
  182.  
  183.    TODO: do reverse loops right */
  184. static Instrument *load_instrument(char *name, int percussion,
  185.                    int panning, int amp, int note_to_use,
  186.                    int strip_loop, int strip_envelope,
  187.                    int strip_tail)
  188. {
  189.   Instrument *ip;
  190.   Sample *sp;
  191.   FILE *fp;
  192.   uint8 tmp[1024];
  193.   int i,j,noluck=0;
  194. #ifdef PATCH_EXT_LIST
  195.   static char *patch_ext[] = PATCH_EXT_LIST;
  196. #endif
  197.  
  198.   if (!name) return 0;
  199.   
  200.   /* Open patch file */
  201.   if (!(fp=open_file(name, 1, OF_NORMAL)))
  202.     {
  203.       noluck=1;
  204. #ifdef PATCH_EXT_LIST
  205.       /* Try with various extensions */
  206.       for (i=0; patch_ext[i]; i++)
  207.     {
  208.       if (strlen(name)+strlen(patch_ext[i])<1024)
  209.         {
  210.           strcpy(tmp, name);
  211.           strcat(tmp, patch_ext[i]);
  212.           if ((fp=open_file(tmp, 1, OF_NORMAL)))
  213.         {
  214.           noluck=0;
  215.           break;
  216.         }
  217.         }
  218.     }
  219. #endif
  220.     }
  221.   
  222.   if (noluck)
  223.     {
  224.       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
  225.         "Instrument `%s' can't be found.", name);
  226.       return 0;
  227.     }
  228.       
  229.   ctl->cmsg(CMSG_INFO, VERB_NOISY, "Loading instrument %s", current_filename);
  230.   
  231.   /* Read some headers and do cursory sanity checks. There are loads
  232.      of magic offsets. This could be rewritten... */
  233.  
  234.   if ((239 != fread(tmp, 1, 239, fp)) ||
  235.       (memcmp(tmp, "GF1PATCH110\0ID#000002", 22) &&
  236.        memcmp(tmp, "GF1PATCH100\0ID#000002", 22))) /* don't know what the
  237.                               differences are */
  238.     {
  239.       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: not an instrument", name);
  240.       return 0;
  241.     }
  242.   
  243.   if (tmp[82] != 1 && tmp[82] != 0) /* instruments. To some patch makers, 
  244.                        0 means 1 */
  245.     {
  246.       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
  247.        "Can't handle patches with %d instruments", tmp[82]);
  248.       return 0;
  249.     }
  250.  
  251.   if (tmp[151] != 1 && tmp[151] != 0) /* layers. What's a layer? */
  252.     {
  253.       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
  254.        "Can't handle instruments with %d layers", tmp[151]);
  255.       return 0;
  256.     }
  257.   
  258.   ip=safe_malloc(sizeof(Instrument));
  259.   ip->samples = tmp[198];
  260.   ip->sample = safe_malloc(sizeof(Sample) * ip->samples);
  261.   for (i=0; i<ip->samples; i++)
  262.     {
  263.  
  264.       uint8 fractions;
  265.       int32 tmplong;
  266.       uint16 tmpshort;
  267.       uint8 tmpchar;
  268.  
  269. #define READ_CHAR(thing) \
  270.       if (1 != fread(&tmpchar, 1, 1, fp)) goto fail; \
  271.       thing = tmpchar;
  272. #define READ_SHORT(thing) \
  273.       if (1 != fread(&tmpshort, 2, 1, fp)) goto fail; \
  274.       thing = LE_SHORT(tmpshort);
  275. #define READ_LONG(thing) \
  276.       if (1 != fread(&tmplong, 4, 1, fp)) goto fail; \
  277.       thing = LE_LONG(tmplong);
  278.  
  279.       skip(fp, 7); /* Skip the wave name */
  280.  
  281.       if (1 != fread(&fractions, 1, 1, fp))
  282.     {
  283.     fail:
  284.       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Error reading sample %d", i);
  285.       for (j=0; j<i; j++)
  286.         free(ip->sample[j].data);
  287.       free(ip->sample);
  288.       free(ip);
  289.       return 0;
  290.     }
  291.  
  292.       sp=&(ip->sample[i]);
  293.       
  294.       READ_LONG(sp->data_length);
  295.       READ_LONG(sp->loop_start);
  296.       READ_LONG(sp->loop_end);
  297.       READ_SHORT(sp->sample_rate);
  298.       READ_LONG(sp->low_freq);
  299.       READ_LONG(sp->high_freq);
  300.       READ_LONG(sp->root_freq);
  301.       skip(fp, 2); /* Why have a "root frequency" and then "tuning"?? */
  302.       
  303.       READ_CHAR(tmp[0]);
  304.  
  305.       if (panning==-1)
  306.     sp->panning = (tmp[0] * 8 + 4) & 0x7f;
  307.       else
  308.     sp->panning=(uint8)(panning & 0x7F);
  309.  
  310.       /* envelope, tremolo, and vibrato */
  311.       if (18 != fread(tmp, 1, 18, fp)) goto fail; 
  312.  
  313.       if (!tmp[13] || !tmp[14])
  314.     {
  315.       sp->tremolo_sweep_increment=
  316.         sp->tremolo_phase_increment=sp->tremolo_depth=0;
  317.       ctl->cmsg(CMSG_INFO, VERB_DEBUG, " * no tremolo");
  318.     }
  319.       else
  320.     {
  321.       sp->tremolo_sweep_increment=convert_tremolo_sweep(tmp[12]);
  322.       sp->tremolo_phase_increment=convert_tremolo_rate(tmp[13]);
  323.       sp->tremolo_depth=tmp[14];
  324.       ctl->cmsg(CMSG_INFO, VERB_DEBUG,
  325.            " * tremolo: sweep %d, phase %d, depth %d",
  326.            sp->tremolo_sweep_increment, sp->tremolo_phase_increment,
  327.            sp->tremolo_depth);
  328.     }
  329.  
  330.       if (!tmp[16] || !tmp[17])
  331.     {
  332.       sp->vibrato_sweep_increment=
  333.         sp->vibrato_control_ratio=sp->vibrato_depth=0;
  334.       ctl->cmsg(CMSG_INFO, VERB_DEBUG, " * no vibrato");
  335.     }
  336.       else
  337.     {
  338.       sp->vibrato_control_ratio=convert_vibrato_rate(tmp[16]);
  339.       sp->vibrato_sweep_increment=
  340.         convert_vibrato_sweep(tmp[15], sp->vibrato_control_ratio);
  341.       sp->vibrato_depth=tmp[17];
  342.       ctl->cmsg(CMSG_INFO, VERB_DEBUG,
  343.            " * vibrato: sweep %d, ctl %d, depth %d",
  344.            sp->vibrato_sweep_increment, sp->vibrato_control_ratio,
  345.            sp->vibrato_depth);
  346.  
  347.     }
  348.  
  349.       READ_CHAR(sp->modes);
  350.  
  351.       skip(fp, 40); /* skip the useless scale frequency, scale factor
  352.                (what's it mean?), and reserved space */
  353.  
  354.       /* Mark this as a fixed-pitch instrument if such a deed is desired. */
  355.       if (note_to_use!=-1)
  356.     sp->note_to_use=(uint8)(note_to_use);
  357.       else
  358.     sp->note_to_use=0;
  359.       
  360.       /* seashore.pat in the Midia patch set has no Sustain. I don't
  361.          understand why, and fixing it by adding the Sustain flag to
  362.          all looped patches probably breaks something else. We do it
  363.          anyway. */
  364.      
  365.       if (sp->modes & MODES_LOOPING) 
  366.     sp->modes |= MODES_SUSTAIN;
  367.  
  368.       /* Strip any loops and envelopes we're permitted to */
  369.       if ((strip_loop==1) && 
  370.       (sp->modes & (MODES_SUSTAIN | MODES_LOOPING | 
  371.             MODES_PINGPONG | MODES_REVERSE)))
  372.     {
  373.       ctl->cmsg(CMSG_INFO, VERB_DEBUG, " - Removing loop and/or sustain");
  374.       sp->modes &=~(MODES_SUSTAIN | MODES_LOOPING | 
  375.             MODES_PINGPONG | MODES_REVERSE);
  376.     }
  377.  
  378.       if (strip_envelope==1)
  379.     {
  380.       if (sp->modes & MODES_ENVELOPE)
  381.         ctl->cmsg(CMSG_INFO, VERB_DEBUG, " - Removing envelope");
  382.       sp->modes &= ~MODES_ENVELOPE;
  383.     }
  384.       else if (strip_envelope != 0)
  385.     {
  386.       /* Have to make a guess. */
  387.       if (!(sp->modes & (MODES_LOOPING | MODES_PINGPONG | MODES_REVERSE)))
  388.         {
  389.           /* No loop? Then what's there to sustain? No envelope needed
  390.          either... */
  391.           sp->modes &= ~(MODES_SUSTAIN|MODES_ENVELOPE);
  392.           ctl->cmsg(CMSG_INFO, VERB_DEBUG, 
  393.             " - No loop, removing sustain and envelope");
  394.         }
  395.       else if (!memcmp(tmp, "??????", 6) || tmp[11] >= 100) 
  396.         {
  397.           /* Envelope rates all maxed out? Envelope end at a high "offset"?
  398.          That's a weird envelope. Take it out. */
  399.           sp->modes &= ~MODES_ENVELOPE;
  400.           ctl->cmsg(CMSG_INFO, VERB_DEBUG, 
  401.             " - Weirdness, removing envelope");
  402.         }
  403.       else if (!(sp->modes & MODES_SUSTAIN))
  404.         {
  405.           /* No sustain? Then no envelope.  I don't know if this is
  406.          justified, but patches without sustain usually don't need the
  407.          envelope either... at least the Gravis ones. They're mostly
  408.          drums.  I think. */
  409.           sp->modes &= ~MODES_ENVELOPE;
  410.           ctl->cmsg(CMSG_INFO, VERB_DEBUG, 
  411.             " - No sustain, removing envelope");
  412.         }
  413.     }
  414.  
  415.       for (j=0; j<6; j++)
  416.     {
  417.       sp->envelope_rate[j]=
  418.         convert_envelope_rate(tmp[j]);
  419.       sp->envelope_offset[j]= 
  420.         convert_envelope_offset(tmp[6+j]);
  421.     }
  422.  
  423.       /* Then read the sample data */
  424.       sp->data = safe_malloc(sp->data_length);
  425.       if (1 != fread(sp->data, sp->data_length, 1, fp))
  426.     goto fail;
  427.       
  428.       if (!(sp->modes & MODES_16BIT)) /* convert to 16-bit data */
  429.     {
  430.       int32 i=sp->data_length;
  431.       uint8 *cp=(uint8 *)(sp->data);
  432.       uint16 *tmp,*new;
  433.       tmp=new=safe_malloc(sp->data_length*2);
  434.       while (i--)
  435.         *tmp++ = (uint16)(*cp++) << 8;
  436.       cp=(uint8 *)(sp->data);
  437.       sp->data = (sample_t *)new;
  438.       free(cp);
  439.       sp->data_length *= 2;
  440.       sp->loop_start *= 2;
  441.       sp->loop_end *= 2;
  442.     }
  443. #ifndef LITTLE_ENDIAN
  444.       else
  445.     /* convert to machine byte order */
  446.     {
  447.       int32 i=sp->data_length/2;
  448.       int16 *tmp=(int16 *)sp->data,s;
  449.       while (i--)
  450.         { 
  451.           s=LE_SHORT(*tmp);
  452.           *tmp++=s;
  453.         }
  454.     }
  455. #endif
  456.       
  457.       if (sp->modes & MODES_UNSIGNED) /* convert to signed data */
  458.     {
  459.       int32 i=sp->data_length/2;
  460.       int16 *tmp=(int16 *)sp->data;
  461.       while (i--)
  462.         *tmp++ ^= 0x8000;
  463.     }
  464.  
  465.       /* Reverse reverse loops and pass them off as normal loops */
  466.       if (sp->modes & MODES_REVERSE)
  467.     {
  468.       int32 t;
  469.       /* The GUS apparently plays reverse loops by reversing the
  470.          whole sample. We do the same because the GUS does not SUCK. */
  471.  
  472.       ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "Reverse loop in %s", name);
  473.       reverse_data((int16 *)sp->data, 0, sp->data_length/2);
  474.  
  475.       t=sp->loop_start;
  476.       sp->loop_start=sp->data_length - sp->loop_end;
  477.       sp->loop_end=sp->data_length - t;
  478.  
  479.       sp->modes &= ~MODES_REVERSE;
  480.       sp->modes |= MODES_LOOPING; /* just in case */
  481.     }
  482.  
  483.       /* If necessary do some anti-aliasing filtering  */
  484.  
  485.       if (antialiasing_allowed)
  486.       antialiasing(sp,play_mode->rate);
  487.  
  488. #ifdef ADJUST_SAMPLE_VOLUMES
  489.       if (amp!=-1)
  490.     sp->volume=(double)(amp) / 100.0;
  491.       else
  492.     {
  493.       /* Try to determine a volume scaling factor for the sample.
  494.          This is a very crude adjustment, but things sound more
  495.          balanced with it. Still, this should be a runtime option. */
  496.       int32 i=sp->data_length/2;
  497.       int16 maxamp=0,a;
  498.       int16 *tmp=(int16 *)sp->data;
  499.       while (i--)
  500.         {
  501.           a=*tmp++;
  502.           if (a<0) a=-a;
  503.           if (a>maxamp)
  504.         maxamp=a;
  505.         }
  506.       sp->volume=32768.0 / (double)(maxamp);
  507.       ctl->cmsg(CMSG_INFO, VERB_DEBUG, " * volume comp: %f", sp->volume);
  508.     }
  509. #else
  510.       if (amp!=-1)
  511.     sp->volume=(double)(amp) / 100.0;
  512.       else
  513.     sp->volume=1.0;
  514. #endif
  515.  
  516.       sp->data_length /= 2; /* These are in bytes. Convert into samples. */
  517.       sp->loop_start /= 2;
  518.       sp->loop_end /= 2;
  519.  
  520.       /* Then fractional samples */
  521.       sp->data_length <<= FRACTION_BITS;
  522.       sp->loop_start <<= FRACTION_BITS;
  523.       sp->loop_end <<= FRACTION_BITS;
  524.  
  525.       /* Adjust for fractional loop points. This is a guess. Does anyone
  526.      know what "fractions" really stands for? */
  527.       sp->loop_start |=
  528.     (fractions & 0x0F) << (FRACTION_BITS-4);
  529.       sp->loop_end |=
  530.     ((fractions>>4) & 0x0F) << (FRACTION_BITS-4);
  531.  
  532.       /* If this instrument will always be played on the same note,
  533.      and it's not looped, we can resample it now. */
  534.       if (sp->note_to_use && !(sp->modes & MODES_LOOPING))
  535.     pre_resample(sp);
  536.  
  537. #ifdef LOOKUP_HACK
  538.       /* Squash the 16-bit data into 8 bits. */
  539.       {
  540.     uint8 *gulp,*ulp;
  541.     int16 *swp;
  542.     int l=sp->data_length >> FRACTION_BITS;
  543.     gulp=ulp=safe_malloc(l+1);
  544.     swp=(int16 *)sp->data;
  545.     while(l--)
  546.       *ulp++ = (*swp++ >> 8) & 0xFF;
  547.     free(sp->data);
  548.     sp->data=(sample_t *)gulp;
  549.       }
  550. #endif
  551.       
  552.       if (strip_tail==1)
  553.     {
  554.       /* Let's not really, just say we did. */
  555.       ctl->cmsg(CMSG_INFO, VERB_DEBUG, " - Stripping tail");
  556.       sp->data_length = sp->loop_end;
  557.     }
  558.     }
  559.  
  560.   close_file(fp);
  561.   return ip;
  562. }
  563.  
  564. static int fill_bank(int dr, int b)
  565. {
  566.   int i, errors=0;
  567.   ToneBank *bank=((dr) ? drumset[b] : tonebank[b]);
  568.   if (!bank)
  569.     {
  570.       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
  571.        "Huh. Tried to load instruments in non-existent %s %d",
  572.        (dr) ? "drumset" : "tone bank", b);
  573.       return 0;
  574.     }
  575.   for (i=0; i<128; i++)
  576.     {
  577.       if (bank->tone[i].instrument==MAGIC_LOAD_INSTRUMENT)
  578.     {
  579.       if (!(bank->tone[i].name))
  580.         {
  581.           ctl->cmsg(CMSG_WARNING, (b!=0) ? VERB_VERBOSE : VERB_NORMAL,
  582.            "No instrument mapped to %s %d, program %d%s",
  583.            (dr)? "drum set" : "tone bank", b, i, 
  584.            (b!=0) ? "" : " - this instrument will not be heard");
  585.           if (b!=0)
  586.         {
  587.           /* Mark the corresponding instrument in the default
  588.              bank / drumset for loading (if it isn't already) */
  589.           if (!dr)
  590.             {
  591.               if (!(standard_tonebank.tone[i].instrument))
  592.             standard_tonebank.tone[i].instrument=
  593.               MAGIC_LOAD_INSTRUMENT;
  594.             }
  595.           else
  596.             {
  597.               if (!(standard_drumset.tone[i].instrument))
  598.             standard_drumset.tone[i].instrument=
  599.               MAGIC_LOAD_INSTRUMENT;
  600.             }
  601.         }
  602.           bank->tone[i].instrument=0;
  603.           errors++;
  604.         }
  605.       else if (!(bank->tone[i].instrument=
  606.              load_instrument(bank->tone[i].name, 
  607.                      (dr) ? 1 : 0,
  608.                      bank->tone[i].pan,
  609.                      bank->tone[i].amp,
  610.                      (bank->tone[i].note!=-1) ? 
  611.                      bank->tone[i].note :
  612.                      ((dr) ? i : -1),
  613.                      (bank->tone[i].strip_loop!=-1) ?
  614.                      bank->tone[i].strip_loop :
  615.                      ((dr) ? 1 : -1),
  616.                      (bank->tone[i].strip_envelope != -1) ? 
  617.                      bank->tone[i].strip_envelope :
  618.                      ((dr) ? 1 : -1),
  619.                      bank->tone[i].strip_tail )))
  620.         {
  621.           ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
  622.            "Couldn't load instrument %s (%s %d, program %d)",
  623.            bank->tone[i].name,
  624.            (dr)? "drum set" : "tone bank", b, i);
  625.           errors++;
  626.         }
  627.     }
  628.     }
  629.   return errors;
  630. }
  631.  
  632. int load_missing_instruments(void)
  633. {
  634.   int i=128,errors=0;
  635.   while (i--)
  636.     {
  637.       if (tonebank[i])
  638.     errors+=fill_bank(0,i);
  639.       if (drumset[i])
  640.     errors+=fill_bank(1,i);
  641.     }
  642.   return errors;
  643. }
  644.  
  645. void free_instruments(void)
  646. {
  647.   int i=128;
  648.   while(i--)
  649.     {
  650.       if (tonebank[i])
  651.     free_bank(0,i);
  652.       if (drumset[i])
  653.     free_bank(1,i);
  654.     }
  655. }
  656.  
  657. int set_default_instrument(char *name)
  658. {
  659.   Instrument *ip;
  660.   if (!(ip=load_instrument(name, 0, -1, -1, -1, 0, 0, 0)))
  661.     return -1;
  662.   if (default_instrument)
  663.     free_instrument(default_instrument);
  664.   default_instrument=ip;
  665.   default_program=SPECIAL_PROGRAM;
  666.   return 0;
  667. }
  668.